package com.xteam.ecsystem.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.service.front.FrontGoodsListService;
import com.xteam.ecsystem.utils.DirectiveUtil;
import com.xteam.ecsystem.utils.PageCenterStation;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 商品列表标签
 * @author ls
 */
@Service
public class GoodsListDirective implements TemplateDirectiveModel {
	
	@Autowired
	private FrontGoodsListService fglService;

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
//		par_goodstype 精品？新品？热销？团购，par_goodsgroup 商品分类，par_index 第几页，par_num 显示数量，par_order 排序字段
//		par_goodsbrand 商品品牌，par_pricemin 最低价格，par_pricemax 最高价格，VAR_SELECTEDATTR 已选查询属性
		String goodstype = DirectiveUtil.getString(params, Constant.PAR_GOODSTYPE.value());
		String goodsgroup = DirectiveUtil.getString(params, Constant.PAR_GOODSGROUP.value());
		String brand = DirectiveUtil.getString(params, Constant.PAR_GOODSBRAND.value());
		String attr = DirectiveUtil.getString(params, Constant.PAR_SELECTEDATTR.value());
		Integer min = DirectiveUtil.getInteger(params, Constant.PAR_PRICEMIN.value());
		Integer max = DirectiveUtil.getInteger(params, Constant.PAR_PRICEMAX.value());
		int pricemin = 0;
		int pricemax = 0;
		if(min!=null){
			pricemin = min;
		}
		if(max!=null){
			pricemax = max;
		}
		int pageindex = DirectiveUtil.getInteger(params, Constant.PAR_PAGEINDEX.value());
		int pagesize = DirectiveUtil.getInteger(params, Constant.PAR_PAGESIZE.value());
		String keywords = DirectiveUtil.getString(params, Constant.PAR_KEYWORDS.value());
//		String order = DirectiveUtil.getString(params, Constant.Par_ORDER.value());
		PageCenterStation<?> p=this.fglService.getGoodsList(goodstype,goodsgroup,brand,attr,pricemin,pricemax,pageindex,pagesize,keywords);
		env.setVariable(Constant.VAR_GOODSLIST.value(),DEFAULT_WRAPPER.wrap(p));
		body.render(env.getOut());
	}
}
